﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKCodeBase.WinForm;
using YJKOpeningCommon.OpeningEX;
using YJKOpeningCreate.ConfigForm;

namespace YJKOpeningCreate.Cmd
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdOpeningRemove : IExternalCommand
	{
		private void InitActiveWorkSetId()
		{
			WorksetTable worksetTable = this.m_doc.GetWorksetTable();
			this.m_activeWorksetId = worksetTable.GetActiveWorksetId();
		}

		private List<OpeningData> GetDocActiveWorkSetOpeningData()
		{
			this.InitActiveWorkSetId();
			List<OpeningData> result = new List<OpeningData>();
			try
			{
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
				filteredElementCollector.OfCategory(BuiltInCategory.OST_GenericModel).WhereElementIsNotElementType();
				this.GetDocActiveWorkSetOpeningData(filteredElementCollector, ref result);
				FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.m_doc);
				filteredElementCollector2.OfClass(typeof(Opening)).WhereElementIsNotElementType();
				this.GetDocActiveWorkSetOpeningData(filteredElementCollector2, ref result);
			}
			catch (Exception)
			{
			}
			return result;
		}

		private void GetDocActiveWorkSetOpeningData(FilteredElementCollector collector, ref List<OpeningData> listOpeningData)
		{
			foreach (Element element in collector)
			{
				if (!(element.WorksetId != this.m_activeWorksetId) && OpeningXData.IsIncludeSchema(element))
				{
					bool flag = false;
					foreach (OpeningData openingData in listOpeningData)
					{
						if (openingData.Equals(element))
						{
							openingData.m_ListOpening3D.Add(element);
							flag = true;
							break;
						}
					}
					if (!flag)
					{
						OpeningData openingData2 = new OpeningData();
						openingData2.OpeningDataIni(element);
						listOpeningData.Add(openingData2);
					}
				}
			}
		}

		public Result Execute(ExternalCommandData cmdData, ref string messages, ElementSet elements)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			this.m_doc = activeUIDocument.Document;
			try
			{
				if (cmdData.Application.ActiveUIDocument.Document.IsModified)
				{
					MessageBox.Show("请先保存项目。");
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				if (CmdOpeningRemove.m_HWndRevit == null)
				{
					Process currentProcess = Process.GetCurrentProcess();
					IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
					CmdOpeningRemove.m_HWndRevit = new WindowHandle(mainWindowHandle);
				}
				this.m_ListOpeningData = this.GetDocActiveWorkSetOpeningData();
				if (this.m_ListOpeningData.Count == 0)
				{
					MessageBox.Show("当前项目无可删除的洞口。");
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				OpeningRemoveForm openingRemoveForm = new OpeningRemoveForm(this.m_doc, CmdOpeningRemove.m_HWndRevit, this.m_ListOpeningData);
				openingRemoveForm.ShowDialog();
			}
			catch
			{
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private WorksetId m_activeWorksetId;

		private Document m_doc;

		private TransactionGroup m_transGroup;

		private List<OpeningData> m_ListOpeningData;

		public static WindowHandle m_HWndRevit;
	}
}
